package com.cdivtc.management.service;

import com.cdivtc.management.dao.*;
import com.cdivtc.management.pojo.*;
import com.cdivtc.management.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;


import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class TeacherTaskService {

    @Autowired
    private TeacherReleaseTaskMapper teacherReleaseTaskMapper;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private FilesurlMapper filesurlMapper;

    @Autowired
    private UploadService uploadService;

    @Autowired
    private TopicTitleMapper topicTitleMapper;


    public TeacherReleaseTask addTeaTask(TeaAddTaskParamBean tt) {
        TeacherReleaseTask tr = new TeacherReleaseTask();
        tr.setAsk(tt.getTaskRequest());
        tr.setType(tt.getTaskType());
        tr.setStopDate(tt.getEndTime());
        tr.setTopicId(tt.getTopicId());
        tr.setContent(tt.getSubmitContent().toString().replace("[", "").replace("]", ""));
        tr.setReleaseDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        tr.setState("0");
        tr.setId(idWorker.nextId() + "");
        tr.setTeaId(tt.getTea_id());
        int insert = teacherReleaseTaskMapper.insert(tr);
        if (insert > 0) {
            List<String> submitFile = tt.getSubmitFile();
            if (submitFile != null) {//给文件加上任务id
                for (String s : submitFile) {
                    Example example = new Example(Filesurl.class);
                    Example.Criteria criteria = example.createCriteria();
                    criteria.andEqualTo("id", s);
                    Filesurl filesurl = filesurlMapper.selectOneByExample(example);
                    filesurl.setTaskId(tr.getId());
                    filesurlMapper.updateByExample(filesurl, example);
                }
            }
            return tr;
        }
        return null;
    }

    public TeacherReleaseTask UpdTeaTask(TeaAddTaskParamBean tt) {
        TeacherReleaseTask tr = new TeacherReleaseTask();
        tr.setAsk(tt.getTaskRequest());
        tr.setType(tt.getTaskType());
        tr.setStopDate(tt.getEndTime());
        tr.setTopicId(tt.getTopicId());
        tr.setContent(tt.getSubmitContent().toString().replace("[", "").replace("]", ""));
        tr.setReleaseDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        tr.setState("0");
        tr.setId(tt.getTaskId());
        tr.setTeaId(tt.getTea_id());

        Example example1 = new Example(TeacherReleaseTask.class);
        Example.Criteria criteria1 = example1.createCriteria();
        criteria1.andEqualTo("id", tt.getTaskId());
        int i = teacherReleaseTaskMapper.updateByExample(tr, example1);
        System.out.println("i = " + i);
        if (i > 0) {
            List<String> submitFile = tt.getSubmitFile();
            if (submitFile != null) {//给文件加上任务id
                for (String s : submitFile) {
                    Example example = new Example(Filesurl.class);
                    Example.Criteria criteria = example.createCriteria();
                    criteria.andEqualTo("id", s);
                    Filesurl filesurl = filesurlMapper.selectOneByExample(example);
                    filesurl.setTaskId(tr.getId());
                    filesurlMapper.updateByExample(filesurl, example);
                }
            }
            return tr;
        }
        return null;
    }


    public void delFile(String userId) {
        Example example4 = new Example(Filesurl.class);
        Example.Criteria criteria4 = example4.createCriteria();
        criteria4.andIsNull("taskId");
        criteria4.andEqualTo("userId", userId);
        List<Filesurl> filesurls = filesurlMapper.selectByExample(example4);
        for (Filesurl filesurl : filesurls) uploadService.deletefile(filesurl.getUrl());
    }

    public List<Map<String, Object>> selAll(String teaId) {
        //查询任务之前把 文件列表为保存的文件删除start
        delFile(teaId);
        //查询任务之前把 文件列表为保存的文件删除end

        ArrayList<Map<String, Object>> maps = new ArrayList<>();

        Example example3 = new Example(TeacherReleaseTask.class);
        Example.Criteria criteria3 = example3.createCriteria();
        criteria3.andEqualTo("teaId", teaId);
        List<TeacherReleaseTask> teacherReleaseTasks = teacherReleaseTaskMapper.selectByExample(example3);

        if (teacherReleaseTasks != null) {
            for (TeacherReleaseTask teacherReleaseTask : teacherReleaseTasks) {
                String topicId = teacherReleaseTask.getTopicId();
                System.out.println("topicId = " + topicId);
                //通过id换title
                Example example2 = new Example(TopicTitle.class);
                Example.Criteria criteria2 = example2.createCriteria();
                criteria2.andEqualTo("id", topicId);
                TopicTitle topicTitle1 = topicTitleMapper.selectOneByExample(example2);
//                teacherReleaseTask.setTopicId(topicTitle1.getTitle());
                teacherReleaseTask.setTopic(topicTitle1);
                HashMap<String, Object> map = new HashMap<>();
                String str1 = teacherReleaseTask.getTeaId();
                String str2 = teacherReleaseTask.getId();
                System.out.println("str2 = " + str1);
                System.out.println("str2 = " + str2);

                Example example1 = new Example(Filesurl.class);
                Example.Criteria criteria1 = example1.createCriteria();

                criteria1.andEqualTo("userId", str1);
                criteria1.andEqualTo("taskId", str2);

                ArrayList<ResultFileUrl> resultFileUrls = uploadService.selAllFileUrlByUserId(teacherReleaseTask.getTeaId(), teacherReleaseTask.getId());

                map.put("task", teacherReleaseTask);
                map.put("file", resultFileUrls);

                maps.add(map);
            }
        }

        return maps;
    }

    public TeacherReleaseTask publishTeaTopic(TeacherReleaseTask tr) {
        System.out.println("tr = " + tr);
        Example example1 = new Example(TeacherReleaseTask.class);
        Example.Criteria criteria1 = example1.createCriteria();
        criteria1.andEqualTo("id", tr.getId());
        TeacherReleaseTask teacherReleaseTask = teacherReleaseTaskMapper.selectOneByExample(example1);
        teacherReleaseTask.setState("1");
        int i = teacherReleaseTaskMapper.updateByExample(teacherReleaseTask, example1);
        if (i > 0) {
            return tr;
        } else {
            return null;
        }
    }


    public TeacherReleaseTask delTeaTopic(TeacherReleaseTask tr) {
        System.out.println("tr = " + tr);
        Example example1 = new Example(TeacherReleaseTask.class);
        Example.Criteria criteria1 = example1.createCriteria();
        criteria1.andEqualTo("id", tr.getId());
        int i = teacherReleaseTaskMapper.deleteByExample(example1);
        Example example2 = new Example(Filesurl.class);
        Example.Criteria criteria2 = example2.createCriteria();
        criteria2.andEqualTo("taskId", tr.getId());
        List<Filesurl> filesurls = filesurlMapper.selectByExample(example2);

        for (Filesurl filesurl : filesurls) {
            System.out.println("filesurl = " + filesurl);
            uploadService.deletefile(filesurl.getUrl());
        }
        if (i > 0) {
            return tr;
        } else {
            return null;
        }
    }


}
